
#include "mainscene.h"
#include "ui_mainscene.h"
#include<QIcon>
#include<QPainter>
#include<QPoint>

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    resize(1500,600);

    //设置游戏图标
    this->setWindowIcon(QIcon(":/Bird/download.ico"));

    //鸟的对象
    this->m_Bird=new Bird;

    //将设定好的鸟的对象放置在窗口上
    this->m_Bird->setParent(this);
    //设置窗口尺寸
    this->setFixedSize(this->m_Bird->width(),this->m_Bird->height());

    //主场景中启动定时器，开了这个，整个程序才开始
    this->m_Bird->running();

    //监听切图的信号
    connect(this->m_Bird,&Bird::changePix,[=](){
        update();//切图了，重新绘画
    });

    //监听拖拽的信号
    connect(this->m_Bird,&Bird::moving,[=](QPoint point){
        this->move(point);

        m_Auto_Pos=point;//不加这句的话拖拽完就回去了
    });

    //创建定时器的对象
    this->timer=new QTimer(this);

    //启动定时器
    this->timer->start(25);

    //若想回来就得获取屏幕上的信息了
    //QDesktopWidget * desk=QApplication::desktop();

    //监听上面那个定时器
    connect(timer,&QTimer::timeout,[=](){
        //如果鼠标没按就一直向前冲！
        if(this->m_Bird->mouseDown!=true)
        {
        this->m_Auto_Pos.setX(this->m_Auto_Pos.x()+5);
        }

        if(this->m_Auto_Pos.x()>1500)
        {
            this->m_Auto_Pos.setX(-this->m_Bird->width());
        }

        //窗口也得移动
        this->move(this->m_Auto_Pos);
    });
}

void MainScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,this->m_Bird->m_Bird_Pix);
}

MainScene::~MainScene()
{
    delete ui;
}


